package essentials;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class UtilsToSort {

    static void setNimbusLAF() {
        try {
            LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
            for (int idx = 0; idx < installedLookAndFeels.length; idx++) {
                if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
                    UIManager.setLookAndFeel(installedLookAndFeels[idx].getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SimpleGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SimpleGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SimpleGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SimpleGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static boolean stringIsValidDouble(String s) {
        boolean stringIsValidDouble;

        // FIXME: if s has trailing / leading blanks, but the rest is a valid Double
        //      this methods does say now, because s must be trimmed anywhere else
        if (s == null || s.isEmpty()) {
            //null is not an Double number
            stringIsValidDouble = false;
        } else {
            System.out.println("s=" + s);
            NumberFormat germanNF = NumberFormat.getInstance(Locale.GERMAN);
            ParsePosition pp = new ParsePosition(0);
            Number number = germanNF.parse(s, pp);
            stringIsValidDouble = (pp.getIndex() == s.length());
        }
        return stringIsValidDouble;
    }

    public static Double calcDoublefromString(String s) {
        NumberFormat myNF = NumberFormat.getInstance(Locale.GERMAN);
        double value;
        Double d = null;
        try {
            Number number = myNF.parse(s);
            value = number.doubleValue();
            d = new Double(value);
            System.out.println(value);
        } catch (ParseException ex) {
            System.err.println("Parse exception: " + ex.getMessage());
            Logger.getLogger(SimpleGUI.class.getName()).log(Level.INFO, "String kann nicht zu double konvertiert werden - null wird zurückgeliefert", ex);
        }
        return d;
    }

    public static DecimalFormat getMySimpleDecimalFormatter() {

        DecimalFormat decimalFormatter = new DecimalFormat(",##0.00");
        return decimalFormatter;
    }
}
